{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Timestamp', 'Email Address', 'Name',\n",
       "       'Twitter Handle (used to post/contact winners)',\n",
       "       'About you (optional, helps with a message)',\n",
       "       'Do you want me to write a message? Or Just my name?',\n",
       "       'Hi. Thanks for being part of the community :)'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./SciPy 2018 Pandas for Everyone Giveaway (Responses) - Form Responses 1.csv')\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "@np.vectorize\n",
    "def clean_twitter(user_twitter, bkup_name):\n",
    "    \"\"\"Clean up user input twitter handles.\n",
    "    \n",
    "    Manually fix non-valid twitter handles.\n",
    "    Put an @ in front of twitter hanles (easier to copy paste winners).\n",
    "    Else just use the provided first name.\n",
    "    \"\"\"\n",
    "    if user_twitter == 'Sorry don’t have one :-(':\n",
    "        user_twitter = np.NaN\n",
    "\n",
    "    if not pd.isnull(user_twitter):\n",
    "        if user_twitter[0] == '@':\n",
    "            return(user_twitter)\n",
    "        else:\n",
    "            return f'@{user_twitter}' # f-strings are awesome!\n",
    "    else:\n",
    "        return bkup_name.split(' ')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "# check function\n",
    "assert clean_twitter('chendaniely', 'daniel chen') == '@chendaniely'\n",
    "assert clean_twitter('@chendaniely', 'daniel chen') == '@chendaniely'\n",
    "assert clean_twitter(np.NaN, 'daniel chen') == 'daniel'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "# use the thing\n",
    "df['twitter'] = clean_twitter(df['Twitter Handle (used to post/contact winners)'], df['Name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check to make sure all values are unique\n",
    "len(set(df['twitter'])) == len(df['twitter'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        @dopplershift\n",
       "1            @pojmanic\n",
       "2                  Jon\n",
       "3          @geo_leeman\n",
       "4         @astro_mattj\n",
       "5     @cyclogenesis_au\n",
       "6             @nywxguy\n",
       "7      @sithlorddevops\n",
       "8               @guenp\n",
       "9          @ahmedaskar\n",
       "10               Chris\n",
       "11     @drjosephhardin\n",
       "12                Mark\n",
       "13          @bchen4096\n",
       "14                Jeff\n",
       "15    @dillonniederhut\n",
       "16            @mwcraig\n",
       "17            @ruetzl1\n",
       "18            @mowanie\n",
       "19         @MomokoJane\n",
       "20       @puruckertom \n",
       "21      @malvikasharan\n",
       "22            @Mudein_\n",
       "23                 Max\n",
       "24              @Futzp\n",
       "25            @ocefpaf\n",
       "26     @leland_mcinnes\n",
       "27    @daniellequinn88\n",
       "28        @BruceWallin\n",
       "Name: twitter, dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# the list of people to sample!\n",
    "df['twitter']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The winners!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27    @daniellequinn88\n",
       "16            @mwcraig\n",
       "12                Mark\n",
       "22            @Mudein_\n",
       "8               @guenp\n",
       "9          @ahmedaskar\n",
       "21      @malvikasharan\n",
       "0        @dopplershift\n",
       "26     @leland_mcinnes\n",
       "13          @bchen4096\n",
       "Name: twitter, dtype: object"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['twitter'].sample(10, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernel_info": {
   "name": "python3"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "nteract": {
   "version": "0.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
